package com.baizhi;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Lucence {
    @Test
    public void testCreateIndex() throws IOException {
        //创建一个索引目录
        FSDirectory dir = FSDirectory.open(new File("./index"));
        //创建索引配置对象
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_44, analyzer);
        //创建索引的写对象
        IndexWriter indexWriter = new IndexWriter(dir, conf);
        //对一个文章创建索引
        //创建一个document文档
        Document document = new Document();
        //八种基本类型 和String field 不分词
        //textField 根据指定分词器分词
        //Store.yes 在元数据取存数据 store.no 不在元数据取存储
        document.add(new StringField("id","1",Field.Store.YES));
        document.add(new StringField("title","孙红光是猪",Field.Store.YES));
        document.add(new StringField("create",new SimpleDateFormat("yyyy-MM-dd").format(new Date()),Field.Store.YES));
        document.add(new StringField("author","李恩万",Field.Store.YES));
        document.add(new TextField("content","我是一头大笨猪,站在风雨中，等待我的主人李恩万给我投食",Field.Store.YES));
        indexWriter.addDocument(document);
        indexWriter.commit();
        indexWriter.close();
    }/**
     *索引搜索
     **/

    @Test
    public void testIndexSearch() throws IOException {

        //创建索引的目录
        FSDirectory dir = FSDirectory.open(new File("./index"));
        //读取索引库的位置
        DirectoryReader directoryReader = DirectoryReader.open(dir);
        //创建索引的搜索的对象
        IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
        //搜索
        //参数1:搜索条件   参数2:要搜索结果的前多少条
        TopDocs topDocs = indexSearcher.search(new TermQuery(new Term("title", "孙红光是猪")), 100);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;//相关度排序

        for (int i = 0; i < scoreDocs.length; i++) {
            //文章在索引库中唯一标识
            int doc = scoreDocs[i].doc;
            System.out.println("当前文章的得分:  "+scoreDocs[i].score);
            Document document = indexSearcher.doc(doc);
            System.out.println(document.get("id"));
            System.out.println(document.get("title"));
            System.out.println(document.get("create"));
            System.out.println(document.get("author"));
            System.out.println(document.get("content"));
            System.out.println("============================================");
        }

    }



}
